﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Globalization;
using System.Windows.Data;

namespace NathansMapInfoProjects.WindowManager.Converters
{
    public sealed class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
                              object parameter, CultureInfo culture)
        {
            try
            {
                var source = new BitmapImage();
                source.BeginInit();
                source.UriSource = new Uri("pack://application:,,," + (string)value, UriKind.Absolute);
                //Without this option, the image never finishes loading if you change the source dynamically.
                source.CacheOption = BitmapCacheOption.OnLoad;
                source.EndInit();
                return source;

                //return new BitmapImage(new Uri();
            }
            catch
            {
                return new BitmapImage();
            }
        }

        public object ConvertBack(object value, Type targetType,
                                  object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
